<template>
  <div class="login-container">
    <div
      style="width: 400px; height: 350px; margin: 150px auto; background-color:rgba(107,149,224,0.5); border-radius: 10px">
      <div style="width: 100%; height: 100px; font-size: 30px; line-height: 80px; text-align: center; color: #4a5ed0">欢迎登录
      </div>
      <div style="margin-top: 5px; text-align: center; height: 320px;">
        <el-form class="form" :model="user">
          <el-form-item>
            <el-input v-model="user.employeeNumber" type="text" style="width: 320px;height:40px" placeholder="请输入工号">
              <template #prefix>
                <el-icon>
                  <User />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="user.password" show-password style="width: 320px;height:40px" placeholder="请输入密码">
              <template #prefix>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div style="display: flex; justify-content: center">
              <el-input v-model="user.verCode" prefix-icon="el-icon-user"
                style="width: 168px; margin-right: 10px;height:40px" placeholder="请输入验证码"></el-input>
              <img :src="captchaUrl" @click="clickImg()" width="140px" height="35px" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button style=" margin-top: 10px" type="primary" @click="login()">登录</el-button>
          </el-form-item>
        </el-form>

      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "Login",
  data() {
    return {
      user: {},
      captchaUrl: ''
    }
  },
  created() {
    this.key = Math.random();
    this.captchaUrl = '/api/captcha?key=' + this.key;
  },
  methods: {
    login() {
      request.post("/user/login?key=" + this.key, this.user).then(res => {
        if (res.code == '200') {
          ElMessage({
            message: '登录成功',
            type: 'success',
          })
          localStorage.setItem("user", JSON.stringify(res.data));
          localStorage.setItem("token", JSON.stringify(res.data.token));
          localStorage.setItem("time", JSON.stringify(new Date().getTime()));
          this.$router.push('/list');
        } else {
          ElMessage({
            message: res.message,
            type: 'error',
          })
          this.key = Math.random();
          this.captchaUrl = '/api/captcha?key=' + this.key;
          this.user.verCode = ''
        }
      })
    },
    clickImg() {
      this.key = Math.random();
      this.captchaUrl = '/api/captcha?key=' + this.key;
    },
    register() {
      this.$router.push("/register")
    }
  }
}
</script>
<style scoped lang="scss">
.login-container {
  height: 100vh;
  overflow: hidden;
  background: url("@/assets/A2.jpg") center no-repeat;
  background-size: cover;
  // background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>